class enemy {
  constructor(board) {
    this.id = board.enemyId
    this.ctx = board.ctx
    this.board = board
    this.enemysBj = 0
    this.x = 950
    this.randomY = Math.random() * 525;
    this.width = 80
    this.height = 80
    this.img = images['enemy']
    this.timerIndex = 0
    this.timerIndex2 = 0
    this.speed = 5
    this.enemybulletObj = board.enemybulletObj
    this.enemyBulletId = board.enemyBulletId
  }
  draw() {
    // 敌机创建
    this.timerIndex++
    if (this.timerIndex > 5) {
      this.timerIndex = 0
      this.enemysBj++
    }
    this.x -= this.speed
    this.ctx.drawImage(this.img, 80 * (this.enemysBj % 4), 0, 80, 80, this.x, this.randomY, this.width, this.height)
    if (this.x < -100) {
      delete this.board.enemyObj[this.id]
    }
  }
}

class enemyBullet {
  constructor(enemy, board) {
    this.board = board;
    this.ctx = enemy.ctx
    this.speedBullet = 0
    this.speedEnemy = 10
    this.imgEnemy = images['enemyBullet']
    this.x = enemy.x
    this.y = enemy.randomY + 30
    this.positionX = 0
    this.width = 30
    this.height = 30
    this.enemy = enemy
    this.timer = null
    this.frame()
  }
  // 发射子弹
  draw() {
    this.speedBullet -= this.speedEnemy
    this.positionX = this.x + this.speedBullet
    this.ctx.drawImage(this.imgEnemy, this.positionX, this.y, this.width, this.height)
    if (this.positionX < 0) {
      this.delete();
    }
  }
  suspend(){
    cancelAnimationFrame(this.timer)
  }
  delete(){
    delete this.board.enemybulletObj[this.board.enemyBulletId]
    cancelAnimationFrame(this.timer)
  }
  frame() {
    this.timer = requestAnimationFrame(() => {
      this.frame()
    })
    this.draw()
  }
}

export { enemy, enemyBullet }